local BagLogic = require("bag.BagLogic")
local ItemLogic = require("bag.ItemLogic")
local Msg = require("core.Msg")
local EquipLogic = require("bag.EquipLogic")
local ExcelItem = require("excel.Item")
local ExtendBoxConf = ExcelItem.extendBox
local GridLogic = require("bag.GridLogic")
local CombatCalc = require("skill.CombatCalc")
local Grid = require("bag.Grid")
local ShenbingLogic = require("shenbing.ShenbingLogic")
local ObjHuman = require("core.ObjHuman")
local RoleFFI = require("role.RoleFFI")
local Buffer = require("buffer.Buffer")
local InnerDataManagerFL = require("innerdata.InnerDataManagerFL")
local InnerDataDefine = require("innerdata.Define")
local Obj = require("core.Obj")
local Status = require("scene.Status")
local SmeltLogic = require("bag.SmeltLogic")
local HorseLogic = require("horse.HorseLogic")
local SystemGift = require("excel.SystemGift").openSys
local MailLogicFL = require("mail.MailLogicFL")
local HorseExcel = require("excel.Horse").stage
local TaskCallbacks = require("task.TaskCallbacks")
local ItemDefine = require("bag.ItemDefine")
local VipLogic = require("vip.VipLogic")
local PetLogic = require("pet.PetLogic")
local BaowuLogic = require("baowu.BaowuLogic")
local sbMainConfig = require("excel.Shenbing").shenbing
local RoleAttr = require("role.RoleAttr")
local PetConfig = require("excel.Pet").pet
local AtticConfig = require("excel.Pet").petAttic
local BaowuConfig = require("excel.Baowu").baowu
local SearchLogic = require("bag.SearchLogic")

function CG_BAG_SORT(human)
	BagLogic.sort(human)
end

function CG_BAG_DISCARD(human,msg)
	BagLogic.discard(human,msg.index,"discard")
end

function CG_BAG_SWAP(human,msg)
	BagLogic.swap(human,msg.srcIndex,msg.tarIndex)	
end

function CG_BAG_DIVIDE(human,msg)
	BagLogic.divide(human,msg.srcIndex,msg.tarIndex,msg.cnt)
end

function CG_BAG_ADD_CAP(human, msg)
	BagLogic.addCap(human, msg.cnt)		
end

function CG_ITEM_USE(human, msg)
	ItemLogic.use(human, msg.index, msg.cnt)
end

function CG_ITEM_USE_BATCH(human, msg)
    local nItemID = msg.itemID
    local nCnt = msg.cnt

    if nCnt <= 0 then
        return
    end

    if nCnt > BagLogic.getItemCount(human, nItemID) then
        return
    end

    local __, nIndexList = BagLogic.getIndexById(human, nItemID, true)
    local nUseCnt = 0
    local nSrcCnt = nCnt
    local nUseResult = false
    local nUseIndexList = {}
    for k, v in pairs(nIndexList) do
        if nCnt > 0 then
            nUseResult, nUseCnt = ItemLogic.use(human, k, nCnt >= v.cnt and v.cnt or nCnt, nil, true)
            if nUseCnt and nUseCnt > 0 and nUseCnt <= nCnt then
                nCnt = nCnt - nUseCnt
                nUseIndexList[k] = v
            end
            if nUseResult ~= true or not nUseCnt or nUseCnt <= 0 then
                break
            end
        else
            break
        end
    end

    if nSrcCnt ~= nCnt then
        for k, v in pairs(nUseIndexList) do
            BagLogic.sendBagItemChange(human, k)
        end

        TaskCallbacks.taskUseItemCB(human, nItemID)
        local showUseDesc = ItemDefine.getValue(nItemID, "showUseDesc")
        local useDesc = ItemDefine.getValue(nItemID, "useDesc")
        if showUseDesc and showUseDesc == 0 and useDesc and #useDesc ~= 0 then
            Broadcast.sendBroadcast(human, Lang.MOUSE, useDesc)
        end
        if ItemLogic.data.reCalc then  --重新计算属性
            ObjHuman.doCalc(human)
        end
    end
end

function CG_ITEM_USE_SKILL(human, msg)
	ItemLogic.useSkillData.tx = msg.tx
	ItemLogic.useSkillData.ty = msg.ty
	ItemLogic.useSkillData.recv_id = msg.recv_id
	ItemLogic.use(human, msg.index, 1)
end

function CG_EQUIP_QUERY(human,msg)
	EquipLogic.CG_EQUIP_QUERY(human,msg.uuid)
end

function CG_EQUIP_PUT_ON(human,msg)
	EquipLogic.CG_EQUIP_PUT_ON(human,msg.index)
end

function CG_EQUIP_PUT_OFF(human,msg)
	EquipLogic.CG_EQUIP_PUT_OFF(human,msg.index,msg.bagIndex)
end

function send_GC_EXEND_BOX_QUERY(human,itemID,id)
	
	local msgRet = Msg.gc.GC_EXEND_BOX_QUERY
	msgRet.itemID = itemID
	
	local conf = ExtendBoxConf[id]
	msgRet.yinliang = conf.yinliang
	msgRet.yuanbao = conf.yuanbao
	GridLogic.makeGrids(conf.grids1,msgRet.grids1)
	GridLogic.makeGrids(conf.grids2,msgRet.grids2)
	GridLogic.makeGrids(conf.grids3,msgRet.grids3)
	
	Msg.send(msgRet,human.fd)
end

function CG_GET_ITEM_INFO(human, msg)
  local sendMsg = Msg.gc.GC_GET_ITEM_INFO
  local cache = {}
  Grid.create(cache, msg.itemID, 1, true)
  Grid.makeItem(cache, sendMsg.itemData, 0, human)
  Msg.send(sendMsg, human.fd)
end

function CG_SMELT_INVITE_HELP(human)
  SmeltLogic.inviteHelp(human)
end
function CG_SMELT_HELP_OTHER(human, msg)
  SmeltLogic.helpOther(human, msg.uuid)
end

function CG_YUANBAO_SMELT(human)
  SmeltLogic.smelt(human)
end
--投入元宝
function CG_YUANBAO_SMELT_PUT_INTO(human)
  SmeltLogic.putInto(human)
end

function AddSystemGift(human, oldLv, newLv)
  for i=1, #SystemGift do
    if oldLv < SystemGift[i].startLv and newLv >= SystemGift[i].startLv and #SystemGift[i].giveItem > 0 then
      if BagLogic.checkCanAddItemList(human, SystemGift[i].giveItem, #SystemGift[i].giveItem) then
        for j=1, #SystemGift[i].giveItem do
          BagLogic.addItem(human, SystemGift[i].giveItem[j][1], 1, SystemGift[i].giveItem[j][2], "system")
        end
      else
        --send mail
        local nGridList = {}
        for j=1, #SystemGift[i].giveItem do
          nGridList[j] = Grid.create(nil, SystemGift[i].giveItem[j][1], SystemGift[i].giveItem[j][2], 1)
        end
        MailLogicFL.CreateNewMailForUser(human._id, "系统开启奖励", "由于您的背包已满，特将系统开启奖励发给您", nGridList)
      end
      break
    end
  end
end

InnerDataManagerFL.Register(InnerDataDefine.INNERDATA_TYPE_SEARCH_OHTHER, SearchLogic.OnSearch)
